<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统用户ID-外键" prop="fkSysUserId">
        <el-input
          v-model="queryParams.fkSysUserId"
          placeholder="请输入系统用户ID-外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="身份证号" prop="idNum">
        <el-input
          v-model="queryParams.idNum"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hr:hrUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hr:hrUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hr:hrUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hr:hrUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hrUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="身份证号" align="center" prop="idNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hr:hrUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hr:hrUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户-附表对话框 -->
    <el-dialog v-dialogDragWidth v-dialogDrag :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="系统用户" prop="fkSysUserId">
          <el-input v-model="form.fkSysUserId" v-show="false"/>
          <el-input placeholder="请选择" readOnly="true" v-model="form.nickName"
                    class="input-with-select">
            <el-button @click="showDialog_selectSysUser" slot="append" icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="身份证号" prop="idNum">
          <el-input v-model="form.idNum" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">履历信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHrUserCv">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHrUserCv">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="hrUserCvList" :row-class-name="rowHrUserCvIndex" @selection-change="handleHrUserCvSelectionChange" ref="hrUserCv">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="排序" prop="orderNum">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderNum" placeholder="请输入序号" />
            </template>
          </el-table-column>

          <el-table-column label="开始月份" prop="beginAt">
            <template slot-scope="scope">
              <el-date-picker
                width="100%"
                v-model="scope.row.beginAt"
                type="month"
                placeholder="请输入开始月份"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </template>
          </el-table-column>

          <el-table-column label="结束月份" prop="endAt">
              <template slot-scope="scope">
                <el-date-picker
                  v-model="scope.row.endAt"
                  type="month"
                  placeholder="请输入结束月份"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </template>
          </el-table-column>

          <el-table-column label="单位名称" prop="companyName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.companyName" placeholder="请输入单位名称" />
            </template>
          </el-table-column>

          <el-table-column label="职务" prop="fkUserIdWorker"  align="center" width="240">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fkSysPostId" v-show="false"/>
              <el-input placeholder="请选择" v-model="scope.row.fkSysPostName"
                        class="input-with-select" >
                <el-button @click="showDialog_selectSysPost(scope)" slot="append" icon="el-icon-search" ></el-button>
              </el-input>
            </template>
          </el-table-column>

          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <select-sys-user ref="component_selectSysUser" @ok="selectSysUser_done"/>
    <select-sys-post ref="component_selectSysPost" @ok="selectSysPost_done"/>

  </div>
</template>


<script>

import { listHrUser, getHrUser, delHrUser, addHrUser, updateHrUser } from "@/api/hr/hrUser";

import selectSysUser from "../../system/user/selectUser";
import selectSysPost from '../../system/post/selectPost'

export default {
  name: "HrUser",
  components: { selectSysUser, selectSysPost },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHrUserCv: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户-附表表格数据
      hrUserList: [],
      // 用户信息-履历表格数据
      hrUserCvList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fkSysUserId: null,
        idNum: null,
      },
      // 表单参数
      form: {
        fkSysUserId: '',
        nickName: ''
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户-附表列表 */
    getList() {
      this.loading = true;
      listHrUser(this.queryParams).then(response => {
        this.hrUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userUuid: null,
        fkSysUserId: null,
        idNum: null,
        remark: null
      };
      this.hrUserCvList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userUuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户-附表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userUuid = row.userUuid || this.ids
      getHrUser(userUuid).then(response => {
        this.form = response.data;
        this.hrUserCvList = response.data.hrUserCvList;
        this.open = true;
        this.title = "修改用户-附表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.hrUserCvList = this.hrUserCvList;
          if (this.form.userUuid != null) {
            console.log(this.form)
            updateHrUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHrUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userUuids = row.userUuid || this.ids;
      this.$modal.confirm('是否确认删除用户-附表编号为"' + userUuids + '"的数据项？').then(function() {
        return delHrUser(userUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 用户信息-履历序号 */
    rowHrUserCvIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 用户信息-履历添加按钮操作 */
    handleAddHrUserCv() {
      let obj = {};
      obj.orderNum = "";
      obj.beginAt = "";
      obj.endAt = "";
      obj.companyName = "";
      obj.fkSysPostId = "";
      obj.fkSysPostName = "";
      obj.remark = "";
      this.hrUserCvList.push(obj);
    },
    /** 用户信息-履历删除按钮操作 */
    handleDeleteHrUserCv() {
      if (this.checkedHrUserCv.length == 0) {
        this.$modal.msgError("请先选择要删除的用户信息-履历数据");
      } else {
        const hrUserCvList = this.hrUserCvList;
        const checkedHrUserCv = this.checkedHrUserCv;
        this.hrUserCvList = hrUserCvList.filter(function(item) {
          return checkedHrUserCv.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHrUserCvSelectionChange(selection) {
      this.checkedHrUserCv = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hr/hrUser/export', {
        ...this.queryParams
      }, `hrUser_${new Date().getTime()}.xlsx`)
    },

    /**
     * --------------------------------------
     * 选择用户
     * --------------------------------------
     */

    /** 显示对话框 - 选择系统用户 */
    showDialog_selectSysUser(form) {
      this.$refs.component_selectSysUser.show();
    },

    /** 选择系统用户 - 完成 */
    selectSysUser_done(user) {
      // this.currRowScope.row.fkUserIdWorker = user.userId
      // this.currRowScope.row.workerName = user.userNickNameAndId
      console.log(user)
      console.log(user.nickName)

      this.form.nickName = user.nickName
      this.form.fkSysUserId = user.userId

    },


    /**
     * --------------------------------------
     * 选择职务
     * --------------------------------------
     */

    /** 显示对话框：选择工人 */
    showDialog_selectSysPost(scope) {
      this.currRowScope = scope
      this.$refs.component_selectSysPost.showDialog();
    },

    /** 选择职务 - 完成 */
    selectSysPost_done(post) {
      this.currRowScope.row.fkSysPostId = post.postId
      this.currRowScope.row.fkSysPostName = post.postName
      console.log(post)
    },


  }
};
</script>
